<script setup>
import PageContainer from '@/views/PageContainer.vue'
import { ref, onMounted } from 'vue'
import { useCategoryListService, useDeleteCategoryService } from '@/api/category.js'
import { dateFormat } from '../../utils/dateFormate.js'
import { Delete, Edit } from '@element-plus/icons-vue'
import CateEditDialog from '@/views/cate/CateEditDialog.vue'
import { ElMessage } from 'element-plus'
const categoryList = ref([])
const getCategoryList = async () => {
  let res = await useCategoryListService()
  categoryList.value = res.data
}
onMounted(() => {
  getCategoryList()
})
const cateDialog = ref()
const onEdit = (row) => {
  cateDialog.value.open(row)
}
const confirmEvent = async (e) => {
  await useDeleteCategoryService(e.id)
  await getCategoryList()
  ElMessage.success('删除成功')
}
const cancelEvent = () => {
  console.log('cancel!')
}
</script>

<template>
  <PageContainer title="分类管理">
    <template #header>
      <el-button type="primary" @click="cateDialog.open({})">新增分类</el-button>
    </template>
    <el-table :data="categoryList" style="width: 100%">
      <el-table-column label="序号" type="index" width="80"></el-table-column>
      <el-table-column label="分类名称" prop="categoryName"></el-table-column>
      <el-table-column label="分类别名" prop="categoryAlisa"></el-table-column>
      <el-table-column label="创建时间" prop="createTime">
        <template #default="scope">
          {{ dateFormat(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="updateTime">
        <template #default="scope">
          {{ dateFormat(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" circle @click="onEdit(scope.row)"></el-button>
          <el-popconfirm
            title="Are you sure to delete this?"
            @confirm="confirmEvent(scope.row)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" circle></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <cateEditDialog ref="cateDialog" @updateCategory="getCategoryList" />
  </PageContainer>
</template>

<style scoped></style>
